package com.bbznb.service.serviceImpl;

import com.alibaba.fastjson2.JSON;
import com.bbznb.entity.Movie;
import com.bbznb.entity.MyOrder;
import com.bbznb.entity.UserData;
import com.bbznb.mapper.OrderMapper;
import com.bbznb.mapper.UserDataMapper;
import com.bbznb.mapper.UserMapper;
import com.bbznb.service.UserService;
import com.bbznb.util.OrderQueryDTO;
import com.bbznb.util.PageData;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.bbznb.common.UserS.USER_LOGIN;

@Service
public class UserServiceImpl implements UserService {
     private static final String REDIS_KEY = "movie_list";
    @Autowired
     private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserDataMapper userDataMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;
    @Override
    public PageData getTickets(Integer page, Integer size) {
        PageData pd = new PageData();
        String movieList = redisTemplate.opsForValue().get(REDIS_KEY);
        List<Movie> movies;
        if (movieList == null) {
            movies = userMapper.getMovies(page, size);
            redisTemplate.opsForValue().set(REDIS_KEY, JSON.toJSONString(movies));
        }
        else {
            movies = JSON.parseArray(movieList, Movie.class);
        }
        List<Movie> collect = movies.stream().skip((page-1)*size).limit(size).collect(Collectors.toList());
        for (Movie movie : collect) {
            movie.setDate(LocalDate.now());
            movie.setTime(LocalTime.now());
        }
        pd.setList(collect);
        pd.setTotal(movies.size());
        pd.setPage(page);
        pd.setSize(size);
        return pd;
    }

    @Override
    public List<Movie> getRanking() {
        List<Movie> list = userMapper.getList();
        for (Movie movie : list) {
            movie.setDate(LocalDate.now());
            movie.setTime(LocalTime.now());
        }
        return list;
    }

    @Transactional
    @Override
    public void buyTickets(Integer id) {
        UserData userData = userDataMapper.selectByPrimaryKey(redisTemplate.opsForValue().get(USER_LOGIN));
        Movie movie = userMapper.getMovieById(id);
        MyOrder order = new MyOrder();
        order.setUsername(redisTemplate.opsForValue().get(USER_LOGIN));
        order.setTitle(movie.getTitle());
        order.setPrice(movie.getPrice().subtract(movie.getDiscount()));
        userMapper.subtractAmount(id);
        userMapper.subtractUser(userData.getUserId(),movie.getPrice());
        orderMapper.insert(order);
    }
@Transactional
    @Override
    public void BuyTickets(String username) {
        Movie movie =  userMapper.seleteByUsername(username);
        UserData userData = userDataMapper.selectByPrimaryKey(redisTemplate.opsForValue().get(USER_LOGIN));
        MyOrder order = new MyOrder();
        order.setUsername(redisTemplate.opsForValue().get(USER_LOGIN));
        order.setTitle(movie.getTitle());
        order.setPrice(movie.getPrice().subtract(movie.getDiscount()));
        userMapper.subtractAmount(movie.getId());
        userMapper.subtractUser(userData.getUserId(),movie.getPrice());
        orderMapper.insert(order);
    }

    @Override
    public List<MyOrder> getOrders(OrderQueryDTO orderQueryDTO) {
        List<MyOrder> orders = orderMapper.selectAll(redisTemplate.opsForValue().get(USER_LOGIN),orderQueryDTO);
        return  orders;
    }

}
